package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Menu implements State {
    public final static String Options = "Options\n(L)ist Books\n(R)eserve Book";
    public final static String InvalidSelectionMessage = "Invalid Selection";
    private final PrintStream printStream;
    private final BufferedReader reader;
    private List<String> libraryBooks;
    private Map<String, State> commandsByMenuOption = new HashMap<String, State>();

    public Menu(PrintStream stream, BufferedReader reader, List<String> libraryBooks) {
        this.printStream = stream;
        this.reader = reader;
        this.libraryBooks = libraryBooks;
        this.commandsByMenuOption.put("l", new ListBooks(printStream, libraryBooks, this));
    }

    public State nextState() {
        String response = response();
        if (!commandsByMenuOption.containsKey(response)){
            printStream.println(InvalidSelectionMessage);
            return null;
        }
        return commandsByMenuOption.get(response);
    }

    private String response() {
        try {
            return reader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "No input";
    }

    @Override
    public void execute() {
        printStream.println(Options);
    }
}
